<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace app\game\controller;

use think\Controller;
use think\Db;
/**
 * Description of CalcController
 *
 * @author Administrator
 */
class CalcController extends Controller{
    public function getServerId(){
        $server = $this->request->param('ip');
        if(empty($server)){
            echo 'error';
            return;
        }
        if(strstr('http', $server)==false){
            $server = preg_replace('/http[s]?:\/\//', '', $server);
        }
        $server = trim($server,'/');
        $sv = Db::name('server')->where('ip_s',$server)->find();
        if($sv){
            echo $sv['id'];
        }else{
           $id = dmt_add_new_server($server);
           echo $id;
        }
        die();
    }
    /**
     * 激活
     */
    public function active(){
        $act = 30;
        $mt = $this->request->param('mt');
        $sid = $this->request->param('sid',0);
        $unid = $this->request->param('unid');
        $userid = $this->request->param('userid',0);
        $ip = get_client_ip();
        if($sid){
            $p_id = 0;
            if($userid <1){
                $last = Db::name('popularize')->where(['serverid'=>$sid,'client_ip'=>$ip])->where('userid','>',0)->order('id','desc')->find();
                if($last){
                    $userid = $last['userid'];
                    $p_id = $last['p_id'];
                }
            }
            $data = ['serverid'=>$sid,'unid'=>$unid,'plat'=>$mt,'client_ip'=>$ip,'userid'=>$userid,'action'=>$act,'p_id'=>$p_id];
            Db::name('popularize')->insert($data);
        }
        echo '1';
        die();
    }
    
    public function create_url(){
        $uid = $this->request->param('uid',0);
        if(empty($uid)){
            $this->result('', 1, '参数代理ID', 'json');
        }
        $ip = get_client_ip();
        $sv = Db::name('server')->where('ip_s',$ip)->find();
        if(empty($sv)){
            $this->result('', 1, 'ip未注册', 'json');
        }
        $svid = $sv['id'];
        $pop = Db::name('popularize_user')->where(['user_id'=>$uid,'serverid'=>$svid])->find();
        if($pop){
            $id = $pop['id'];
        }else{
            $id = Db::name('popularize_user')->insert(['user_id'=>$uid,'serverid'=>$svid], false, true);
        }
        $url = 'http://demo.3138.com/yyb.html?x='.$id;
        $this->result(['url'=>$url],0, '获取成功', 'json');
    }
    /**
     * 用户推广进入的地址,然后跳转...
     * 记录用户的信息,ip 浏览器,操作系统,推广码
     */
    public function click(){
        $act = 10;
        $x = $this->request->param('x',0);
        $pu = Db::name('popularize_user')->find($x);
        $ref = '';
        if(empty($pu)){
            $this->assign('refresh','');
            return $this->fetch();
        }
        $sid = $pu['serverid'];
        $userid = $pu['user_id'];
        $ip = get_client_ip();
        $ua = $this->request->header('User-Agent');
        $pt = $this->chromePlat($ua);
        $data = ['serverid'=>$sid,'chrome'=>$ua,'plat'=>$pt,'client_ip'=>$ip,'userid'=>$userid,'action'=>$act,'p_id'=>$x];
        Db::name('popularize')->insert($data);
        $url = $pu['download_url'];
        if($url){
            $ref = '<meta http-equiv="refresh" content="0;url='.$url.'">';
        }
        $this->assign('refresh',$ref);
        $this->assign('url',$url);
        return $this->fetch();
    }
    
    /**
     * 玩家点击下载的时候触发,js ajax
     */
    public function download(){
        $act = 20;
        $x = $this->request->param('x',0);
        $pu = Db::name('popularize_user')->find($x);
        if(empty($pu)){
            echo '0';
            die();
        }
        $sid = $pu['serverid'];
        $userid = $pu['user_id'];
        $ip = get_client_ip();
        $ua = $this->request->header('User-Agent');
        $pt = $this->chromePlat($ua);
        $data = ['serverid'=>$sid,'chrome'=>$ua,'plat'=>$pt,'client_ip'=>$ip,'userid'=>$userid,'action'=>$act,'p_id'=>$x];
        Db::name('popularize')->insert($data);
        echo '1';
        die();
    }
    /**
     * 关键一步,注册,客户服务器提供客户端IP,平台,服务器IP/ID
     * 找到能关联的推广员,返回推广码 
     */
    public function regist(){
        $act = 40;
        $serverid = $this->request->param('sid',0);
        if($serverid==0){
            $sv = Db::name('server')->where('ip_s', get_client_ip())->find();
            if($sv){
                $serverid = $sv['id'];
            }
        }
        if($serverid==0){
            $this->result(0,1,'服务器未注册','json');
        }
        $new_uid = $this->request->param('uid');
        $ua = $this->request->param('unid');
        $client_ip = $this->request->param('client_ip');
        $list = Db::name('popularize')->where(['client_ip'=>$client_ip,'serverid'=>$serverid])->order('id','desc')->limit(0,10)->select();
        $uid=0;
        if($list){
            //有推广信息
            foreach ($list as $data){
                if($data['userid']){
                    $uid = $data['userid'];
                    break;
                }
            }
        }
        if($uid){
            $data = ['serverid'=>$serverid,'unid'=>$ua,'client_ip'=>$client_ip,'userid'=>$uid,'action'=>$act,'new_uid'=>$new_uid];
            Db::name('popularize')->insert($data);
            $this->result($uid,0,'服务器未注册','json');
        }
        $this->result(0,1,'找不到推广者','json');
    }
    
    private function chromePlat($user_agency){
        if($user_agency){
            if(stripos('iPhone', $user_agency)>0){
                return 'i';
            }
            if(stripos('Android', $user_agency)>0){
                return 'a';
            }
        }
        return 'x';
    }
}
